package com.yun.util.yun.utils.file;



import com.yun.util.yun.utils.string.SpringContextHolder;
import org.springframework.core.env.Environment;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class OutputFile {

    public String createFile(String fileName) {

        //confirm path exist
        //%tempFilie%/yyyy-dd-MM
        Environment environment = SpringContextHolder.getBean(Environment.class);
        String dirPath = environment.getProperty("tempFile");
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        dir = new File(dir, "idr");
        if (!dir.exists()) {
            dir.mkdirs();
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dataFile = df.format(System.currentTimeMillis());

        dir = new File(dir, dataFile);
        if (!dir.exists()) {
            dir.mkdirs();
        }


        File targetFile = new File( dir, fileName);
        if(!targetFile.exists()){
            try {
                targetFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String path = targetFile.getPath().replace("\\","/");
        return path;
    }
}
